﻿using System;
using System.Collections;
using System.Collections.Generic;
using Assets.Scripts.GtsScripts;
using Assets.Scripts.ProceduralCityGenerator;
using UnityEngine;

// Token: 0x02000344 RID: 836
public class CityPopulationManager : MonoBehaviour
{
	// Token: 0x1700036C RID: 876
	// (get) Token: 0x060015B8 RID: 5560 RVA: 0x0008D734 File Offset: 0x0008B934
	// (set) Token: 0x060015B7 RID: 5559 RVA: 0x0008D72B File Offset: 0x0008B92B
	public Vector3 CityCenter { get; private set; }

	// Token: 0x1700036D RID: 877
	// (get) Token: 0x060015BA RID: 5562 RVA: 0x0008D745 File Offset: 0x0008B945
	// (set) Token: 0x060015B9 RID: 5561 RVA: 0x0008D73C File Offset: 0x0008B93C
	public List<CityBuilding> Buildings { get; private set; }

	// Token: 0x1700036E RID: 878
	// (get) Token: 0x060015BC RID: 5564 RVA: 0x0008D756 File Offset: 0x0008B956
	// (set) Token: 0x060015BB RID: 5563 RVA: 0x0008D74D File Offset: 0x0008B94D
	public float ActiveDistance { get; private set; }

	// Token: 0x1700036F RID: 879
	// (get) Token: 0x060015BE RID: 5566 RVA: 0x0008D767 File Offset: 0x0008B967
	// (set) Token: 0x060015BD RID: 5565 RVA: 0x0008D75E File Offset: 0x0008B95E
	public float CityScale { get; private set; }

	// Token: 0x060015BF RID: 5567 RVA: 0x0008D76F File Offset: 0x0008B96F
	public void SetParameters(Vector3 cityCenter, Vector3 citySize, GameObject buildingsRoot, float cityScale)
	{
		this.Managers = new Dictionary<Humanoid, ActorPopulationManager>();
		this.Buildings = new List<CityBuilding>();
		this.CityCenter = cityCenter;
		this.CityScale = cityScale;
		this.ActiveDistance = citySize.x;
		Debug.Log("Parameters are set for city auto population");
	}

	// Token: 0x060015C0 RID: 5568 RVA: 0x0008D7AD File Offset: 0x0008B9AD
	public void RemoveBuilding(CityBuilding building)
	{
		this.Buildings.Remove(building);
	}

	// Token: 0x060015C1 RID: 5569 RVA: 0x0008D7BC File Offset: 0x0008B9BC
	public void SetTriggerZone(GameObject zone)
	{
		this.CityCollider = zone.GetComponent<CityActorCollider>();
		this.CityCollider.RegisterCityHandler(this);
	}

	// Token: 0x060015C2 RID: 5570 RVA: 0x0008D7D6 File Offset: 0x0008B9D6
	public void AddBuilding(CityBuilding building)
	{
		this.Buildings.Add(building);
	}

	// Token: 0x060015C3 RID: 5571 RVA: 0x0008D7E4 File Offset: 0x0008B9E4
	public void PopStart()
	{
		this.routine = this.UpdateCity();
		base.StartCoroutine(this.routine);
	}

	// Token: 0x060015C4 RID: 5572 RVA: 0x0008D800 File Offset: 0x0008BA00
	private IEnumerator UpdateCity()
	{
		for (;;)
		{
			yield return this.wait;
			this.RescanBuildings();
			yield return this.wait;
			int slow = 0;
			int fast = 0;
			int pop = 0;
			foreach (ActorPopulationManager actorPopulationManager in this.Managers.Values)
			{
				pop += actorPopulationManager.ActiveMicros.Count;
			}
			yield return this.wait;
			foreach (ActorPopulationManager actor in this.Managers.Values)
			{
				if (actor.ActorUpdate(this.Managers.Count, pop))
				{
					fast++;
				}
				else
				{
					slow++;
				}
				yield return null;
			}
			if (fast > slow)
			{
				yield return this.shortWait;
			}
			else
			{
				yield return this.longWait;
			}
		}
		yield break;
	}

	// Token: 0x060015C5 RID: 5573 RVA: 0x0008D81B File Offset: 0x0008BA1B
	private void RescanBuildings()
	{
		this.Buildings.RemoveAll((CityBuilding x) => x.broken);
	}

	// Token: 0x060015C6 RID: 5574 RVA: 0x0008D848 File Offset: 0x0008BA48
	private void OnDestroy()
	{
		base.StopAllCoroutines();
		foreach (ActorPopulationManager actorPopulationManager in this.Managers.Values)
		{
			actorPopulationManager.Destroy();
		}
		UnityEngine.Object.Destroy(this.CityCollider);
	}

	// Token: 0x060015C7 RID: 5575 RVA: 0x0008D8BC File Offset: 0x0008BABC
	public void RegisterActor(Humanoid actor)
	{
		if (actor.isPlayer)
		{
			if (!this.Managers.ContainsKey(actor))
			{
				Debug.Log("Located player in city, attaching handler");
				ActorPopulationManager value = new ActorPopulationManager(this, actor as Player);
				this.Managers.Add(actor, value);
			}
		}
		else if (actor.isGiantess && !this.Managers.ContainsKey(actor))
		{
			Debug.Log("Located a gts, attaching handler");
			ActorPopulationManager value2 = new ActorPopulationManager(this, actor as Giantess);
			this.Managers.Add(actor, value2);
		}
	}

	// Token: 0x060015C8 RID: 5576 RVA: 0x0008D950 File Offset: 0x0008BB50
	public void UnregistorActor(Humanoid actor)
	{
		ActorPopulationManager actorPopulationManager;
		if (this.Managers.TryGetValue(actor, out actorPopulationManager))
		{
			actorPopulationManager.Destroy();
			this.Managers.Remove(actor);
		}
	}

	// Token: 0x04001871 RID: 6257
	private Dictionary<Humanoid, ActorPopulationManager> Managers;

	// Token: 0x04001872 RID: 6258
	private const int cCountdownDuration = 2;

	// Token: 0x04001873 RID: 6259
	private CityActorCollider CityCollider;

	// Token: 0x04001874 RID: 6260
	private IEnumerator routine;

	// Token: 0x04001879 RID: 6265
	private WaitForSeconds shortWait = new WaitForSeconds(1f);

	// Token: 0x0400187A RID: 6266
	private WaitForSeconds longWait = new WaitForSeconds(2f);

	// Token: 0x0400187B RID: 6267
	private WaitForFixedUpdate wait = new WaitForFixedUpdate();
}
